//-----------------------------------------------------------------------
// <copyright file="TagDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class TagDAOBase
    {
        #region Common methods
        public virtual Tag CreateTagFromReader(IDataReader reader)
        {
            Tag item = new Tag();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("TagId"))) item.TagId = (long)reader["TagId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTagFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateTag methods
            
        public virtual void CreateTag(Tag tag)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTagInsert");
                
                database.AddInParameter(dbCommand, "@Title", DbType.String, tag.Title);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tag.CreatedDate);
                database.AddOutParameter(dbCommand, "@TagId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                tag.TagId = (long)database.GetParameterValue(dbCommand, "@TagId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateTagException, ex);
            }
        }

        #endregion

        #region UpdateTag methods
        
        public virtual void UpdateTag(Tag tag)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTagUpdate");            
                
                database.AddInParameter(dbCommand, "@TagId", DbType.Int64, tag.TagId);
                database.AddInParameter(dbCommand, "@Title", DbType.String, tag.Title);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, tag.CreatedDate);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateTagException, ex);
            }
        }
        
        #endregion

        #region DeleteTag methods
        public virtual void DeleteTag(long tagId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTagDelete");
                
                database.AddInParameter(dbCommand, "@TagId", DbType.Int64, tagId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteTagException, ex);
            }
        }

        #endregion

        #region GetTag methods
        
        public virtual Tag GetTag(long tagId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTagGet");
                
                database.AddInParameter(dbCommand, "@TagId", DbType.Int64, tagId);
                
                Tag tag =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        tag = CreateTagFromReader(reader);
                        reader.Close();
                    }
                }
                return tag;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTagException, ex);
            }
        }
        
        #endregion

        #region GetTagList methods
        public virtual TagCollection GetTagList(TagColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTagGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                TagCollection tagCollection = new TagCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Tag tag = CreateTagFromReader(reader);
                        tagCollection.Add(tag);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tagCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTagListException, ex);
            }
        }
        
        public virtual TagCollection GetTagList(TagColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetTagList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetTagList methods
        public virtual TagCollection GetTagListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spTagGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                TagCollection tagCollection = new TagCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Tag tag = CreateTagFromReader(reader);
                        tagCollection.Add(tag);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return tagCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetTagListException, ex);
            }
        }
        
        public virtual TagCollection GetTagListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetTagListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}